<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

class StuController extends Controller
{
    //浏览信息
    public function index(){
        $list = \DB::table('stu')->get();
        //dd($list);
        return view('stu.index', ['list' => $list]);
    }
    //加载添加表单
    public function add(){
        return view("stu.add");
    }
    //添加
    public function store(Request $request){
        //$data = $request->except(['_token']);
        //获取要添加的数据
        $data = ['name'=>$request->name,'age'=>$request->age,'sex'=>$request->sex,'classid'=>$request->classid];
        //dd($data);
        //执行添加
        //$m = \DB::table("stu")->insert($data);  //返回影响行数
        $m = \DB::table("stu")->insertGetId($data);//返回自增id
        if($m>0){
            return "添加成功; 自增id=".$m;
        }else{
            return "添加失败！";
        }

    }
    //浏览单挑信息
    public function show($id){
        $data = \DB::table('stu')->where('id',$id)->first();
        return view('stu.edit', ['stu' => $data]);
      }
    //加载修改表单
    public function edit($id){
        $data = \DB::table('stu')->where('id',$id)->first();
        dd($data);
        //return view('stu.edit', ['stu' => $data]);
    }
    //修改
    public function update(Request $request){

        $data = $request->except(['_token','_method']);

        $m = \DB::table('stu')->where('id',$data['id'])->update($data);
        if($m>0){
            $str = "修改成功; id=".$m;
            return view('stu.info', ['info' => $str]);
        }else{
            return view('stu.info', ['info' => "修改失败！"]);
        }
    }
    //删除
    public function destroy($id){
       $m = \DB::table('stu')->where('id','=',$id)->delete();
        if($m>0){
            $str = "删除成功; id=".$m;
            return view('stu.info', ['info' => $str]);
        }else{
            return view('stu.info', ['info' => "删除失败！"]);
        }
    }

}
